package com.hm.controller;


import com.hm.pojo.HmUser;
import com.hm.serivce.HmUserService;
import com.hm.utils.HmResult;
import com.hm.utils.HttpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@Api(value = "用户登录接口",tags = "用户登录接口")
public class HmUserController {

    @Autowired
    private HmUserService hmUserService;

    /**
     * 注册用户
     * @param hmUser
     * @return
     */
    @ApiOperation(value = "注册用户")
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public HmResult saveUser(HmUser hmUser){
        hmUser.setUserid((int) (Math.random()*100000));
        hmUser.setHand("Null");
        hmUser.setUserstatus("1");
        hmUserService.saveUser(hmUser);
        return HmResult.ok("注册成功!");
    }

    /**
     * 根据用户名密码登录
     * @param username
     * @param password
     * @return
     */
    @ApiOperation(value = "根据用户名密码登录")
    @RequestMapping(value = "/loginHm",method = RequestMethod.POST)
    public HmResult loginHm(String username, String password, HttpServletRequest request){
        HmUser hmUser = hmUserService.loginHm(username,password);
        request.setAttribute("userid",hmUser.getUserid());
        return HmResult.ok(hmUser);
    }

    @ApiOperation(value = "获取验证码")
    @RequestMapping(value = "/verificationCodeLoginUi",method = RequestMethod.POST)
    public List<String> verificationCodeLogin(String usertelephone, HttpSession session){

        HmUser usert = hmUserService.queryHm(usertelephone);

        if (usert!=null){
            String host = "https://smsapi.api51.cn";
            String path = "/code/";
            String method = "GET";
            String appcode = "166c8c4039b14a679a59190b523ca5ae";
            Map<String, String> headers = new HashMap<String, String>();
            //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
            headers.put("Authorization", "APPCODE " + appcode);
            System.out.println(headers);
            Map<String, String> querys = new HashMap<String, String>();
            Random rand = new Random();
            int randNum = rand.nextInt(4999)+5000;
            String code = Integer.toString(randNum);
            querys.put("code", code);
            querys.put("mobile", usertelephone);
            System.out.println(querys);
            session.setAttribute(usertelephone,code);
            /*session.setMaxInactiveInterval(60);*/
            try {
                /**
                 * 重要提示如下:
                 * HttpUtils请从
                 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
                 * 下载
                 *
                 * 相应的依赖请参照
                 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
                 */
                HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
                System.out.println(response.toString());
                //获取response的body
                //System.out.println(EntityUtils.toString(response.getEntity()));
            } catch (Exception e) {
                System.out.println(123);
                e.printStackTrace();
            }
            List<String> list= new ArrayList<>();
            list.add(code);
            list.add(usertelephone);
            System.out.println(list);
            return list;
        }else {
            List<String> list= new ArrayList<>();
            list.add("该电话暂未绑定用户");
            return list;
        }

    }
    @ApiOperation(value = "验证码登录")
    @RequestMapping(value = "/verificationCodeLogins",method = RequestMethod.POST)
    public HmResult verificationCodeLogins(String usertelephone, String code, HttpSession session){
        String a = null;
        System.out.println(123);
        String code1 = null;


        int code3 = Integer.parseInt(code1);
        int code2 = Integer.parseInt(code);
        System.out.println(code);
        if (code3 == code2){
            return HmResult.build(200,"登录成功");
        }else {
            System.out.println(123123);
            return HmResult.build(200,"登录失败，验证码输入错误");
        }

    }

}
